package com.inno.subject.controller.controller;

import com.google.common.base.Preconditions;
import com.inno.subject.common.entity.CommonReturnType;
import com.inno.subject.controller.convert.SubjectCategoryDTOConverter;
import com.inno.subject.controller.dto.SubjectCategoryDTO;
import com.inno.subject.domain.entity.SubjectCategoryBO;
import com.inno.subject.domain.service.SubjectCategoryDomainService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/subject/category")
public class SubjectCategoryController {
    @Resource
    private SubjectCategoryDomainService subjectCategoryService;

    /**
     * 查询分类树
     */
    @PostMapping("/queryList")
    public CommonReturnType<List<SubjectCategoryDTO>> queryList(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {

        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
        List<SubjectCategoryBO> boList = this.subjectCategoryService.queryList(subjectCategoryBO);

        List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCategoryDTOConverter.INSTANCE.convertBOListToDTOList(boList);
        return CommonReturnType.success(subjectCategoryDTOS);

    }
    @PostMapping("/add")
    public CommonReturnType<Void> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        Preconditions.checkArgument(!StringUtils.isBlank(subjectCategoryDTO.getCategoryName()),"分类名称不能为空");
        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
        this.subjectCategoryService.add(subjectCategoryBO);
        return CommonReturnType.success();
    }
    @PostMapping("del")
    public CommonReturnType<Void> del(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        Preconditions.checkNotNull(subjectCategoryDTO.getId(),"分类id不能为空");
        this.subjectCategoryService.delete(subjectCategoryDTO.getId());
        return CommonReturnType.success();
    }
}
